package fxyOffer;


public class StrToNum {
	public static void main(String[] args) {
		System.out.println(StrToNum.strToNumMethod("-fds121ds223"));
		System.err.println(StrToNum.getMaxFromStr("12132fds121ds4560"));
	}
	public static Integer strToNumMethod(String str){
		if(str == null || str.equals("") ){
			return null;
		}
		int num = 0;
		char [] ch = str.toCharArray();
		int i = 0;
		boolean minus = false;
		if(ch[0]=='+')
		{
			i=1;
		}else if(ch[0]=='-')
		{
			minus = true;
			i=1;
		}else{
			i = 0;
		}
		for(int length = str.length(); i < length; i++){
			if('0' <= ch[i] && ch[i] <= '9'){
				num = num*10 + (int)(ch[i] - '0');
			}else{
				return null;
			}
		}
		if(minus){
			num = -num;
		}
		return num;
	}
	public static Integer getMaxFromStr(String str){
		if(str == null || str.equals("") ){
			return null;
		}
		int num = 0;
		int max = Integer.MIN_VALUE;
		char [] ch = str.toCharArray();
		for(int i = 0, length = str.length(); i < length; i++){
			if('0' <= ch[i] && ch[i] <= '9'){
				num = num*10 + (int)(ch[i] - '0');
				if(num > max){
					max = num;
				}
			}else{
				num = 0;
			}
		}
		return max;
	}
}
